package com.example.demo.controller;

import com.example.common.entity.UserInfo;
import com.example.common.tools.AjaxResult;
import com.example.common.tools.UserSessionUtils;
import com.example.demo.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    @RequestMapping("/select")
    public AjaxResult select(HttpServletRequest request, Integer followId) {
        //非控校验
        if(followId == null) {
            return AjaxResult.fail(403, "参数错误1");
        }
        //获取当前登录用户信息(发起关注行为的用户)
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
        !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(403, "当前未登录!");
        }
        //增加关注信息
        return AjaxResult.success(followService.select(userInfo.getId(), followId));
    }

    @RequestMapping("/cancel")
    public AjaxResult cancel(HttpServletRequest request, Integer followId) {
        //非控校验
        if(followId == null) {
            return AjaxResult.fail(403, "参数错误1");
        }
        //获取当前登录用户信息(发起取关行为的用户)
        UserInfo userInfo = UserSessionUtils.getUser(request);
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(403, "当前未登录!");
        }
        return AjaxResult.success(followService.cancel(userInfo.getId(), followId));
    }

}
